package ciyuan.Network;

import ciyuan.MainClass;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class NetWorkRegister {

    public static SimpleChannel INSTANCE;
    public static final String VERSION = "11";
    private static int ID = 0;

    public static int nextID() {
        return ID++;
    }

    @SubscribeEvent
    public static void onCommonSetup(FMLCommonSetupEvent event) {
        event.enqueueWork(NetWorkRegister::registerMessage);
    }

    public static void registerMessage() {
        INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(MainClass.MODID, "main"), () -> {
            return "11";
        }, "11"::equals, "11"::equals);
        INSTANCE.messageBuilder(LivingPacket.class, nextID()).encoder(LivingPacket::toBytes).decoder(LivingPacket::new).consumer(LivingPacket::handler).add();
        INSTANCE.messageBuilder(AddAttrbutePacket.class, nextID()).encoder(AddAttrbutePacket::toBytes).decoder(AddAttrbutePacket::new).consumer(AddAttrbutePacket::handler).add();
        INSTANCE.messageBuilder(ContainerOpenPacket.class, nextID()).encoder(ContainerOpenPacket::toBytes).decoder(ContainerOpenPacket::new).consumer(ContainerOpenPacket::handler).add();
        INSTANCE.messageBuilder(SkillCastPacket.class, nextID()).encoder(SkillCastPacket::toBytes).decoder(SkillCastPacket::new).consumer(SkillCastPacket::handler).add();
    }

}
